package com.tadpole.music.presenter.question;

import com.tadpole.music.bean.question.RealQuestionBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.question.RealQuestionIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 实战真题
 * Created By 孙岩
 * on 2020/9/17
 */
public class RealQuestionPresenter extends BasePresenter<RealQuestionIView> {
    @SuppressWarnings("unchecked")
    public void getRealQuestion(String college_id, String rank_id, int page) {
        if (isViewAttached()) {
            String url = UrlConfig.getRealQuestion + "?college_id=" + college_id + "&rank_id=" + rank_id + "&page=" + page;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    JSONArray array = new JSONArray(object.getString("data"));
                                    List<RealQuestionBean.DataBeanX.DataBean> list = new ArrayList<>();
                                    for(int i=0;i<array.length();i++){
                                        JSONObject jsonObject = new JSONObject(array.get(i).toString());
                                        RealQuestionBean.DataBeanX.DataBean bean = new RealQuestionBean.DataBeanX.DataBean(
                                                JsonParseUtil.getInt(jsonObject,"id"),
                                                JsonParseUtil.getStr(jsonObject,"created_at"),
                                                JsonParseUtil.getStr(jsonObject,"updated_at"),
                                                JsonParseUtil.getStr(jsonObject,"title"),
                                                JsonParseUtil.getInt(jsonObject,"college_id"),
                                                JsonParseUtil.getInt(jsonObject,"question_rank_id"),
                                                JsonParseUtil.getInt(jsonObject,"question_class_id"),
                                                JsonParseUtil.getInt(jsonObject,"question_type_id"),
                                                JsonParseUtil.getInt(jsonObject,"status"),
                                                JsonParseUtil.getStr(jsonObject,"title_img"),
                                                JsonParseUtil.getStr(jsonObject,"title_sound"),
                                                JsonParseUtil.getStr(jsonObject,"bg_sound"),
                                                JsonParseUtil.getInt(jsonObject,"play_times"),
                                                JsonParseUtil.getInt(jsonObject,"interval"),
                                                JsonParseUtil.getInt(jsonObject,"answer_type"),
                                                JsonParseUtil.getStr(jsonObject,"answer_a"),
                                                JsonParseUtil.getStr(jsonObject,"answer_b"),
                                                JsonParseUtil.getStr(jsonObject,"answer_c"),
                                                JsonParseUtil.getStr(jsonObject,"answer_d"),
                                                JsonParseUtil.getStr(jsonObject,"answer_e"),
                                                JsonParseUtil.getStr(jsonObject,"answer_f"),
                                                JsonParseUtil.getStr(jsonObject,"answer_g"),
                                                JsonParseUtil.getStr(jsonObject,"answer_h"),
                                                JsonParseUtil.getInt(jsonObject,"right_answer"),
                                                JsonParseUtil.getStr(jsonObject,"extra"),
                                                JsonParseUtil.getBoolean(jsonObject,"isIs_multy_answer"),
                                                JsonParseUtil.getStr(jsonObject,"multy_right_answer"),
                                                JsonParseUtil.getStr(jsonObject,"left_drum"),
                                                JsonParseUtil.getStr(jsonObject,"right_drum")

                                        );
                                        list.add(bean);
                                    }
                                    RealQuestionBean.DataBeanX bean = new RealQuestionBean.DataBeanX(
                                            JsonParseUtil.getInt(object,"current_page"),
                                            JsonParseUtil.getInt(object,"last_page"),
                                            JsonParseUtil.getInt(object,"page_size"),
                                            JsonParseUtil.getInt(object,"total"),
                                            list
                                    );
                                    getView().showRealQuestion(bean);
                                }else if(obj.getInt("code")==403){
                                    getView().showGoPay();
                                }else if(obj.getInt("code")==401){
                                    getView().show401();
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
